/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.beans.java;

import ca.trader.except.UserNotAuthenticatedException;
import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import ca.trader.utils.HttpUtils;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ahsan
 */
public class LookupIfSaleFavouritedByAuthenticatedUserBean
{

	 private static final String _className = LookupIfSaleFavouritedByAuthenticatedUserBean.class.getName();
	 private boolean resultsFound = false;
	 private boolean saleFavouritedByUser = false;
	 private HttpSession session;

	 public void setSession(HttpSession sess)
	 {
		  this.session = sess;
	 }

	 public void setSale(Sale sale)
	 {
		  final String _methodName = "setSale";


		  TraderLogger.logMethodEntry(_className, _methodName);


		  if (sale == null || session == null)
		  {
			   TraderLogger.logInfo(_className, _methodName, "sale or HttpSession is null. Doing nothing");
		  }
		  else
		  {
			   TraderLogger.logInfo(_className, _methodName, "Finding if sale favourited by authenticated user: " + sale.getId());
			   try
			   {
				    resultsFound = false;

				    Integer authenticatedUserId = HttpUtils.getAuthenticatedUserID(session);
				    saleFavouritedByUser = sale.isFavouritedByUserID(authenticatedUserId);
				    resultsFound = true;
				    
				    
			   TraderLogger.logInfo(_className, _methodName, "Found results.  Sale favourited: " + saleFavouritedByUser);
			   }
			   catch (UserNotAuthenticatedException ex)
			   {
				    TraderLogger.logWarning(_className, _methodName, "No authenticated user found.");
			   }
			   catch (Exception ex)
			   {
				    TraderLogger.logSevere(_className, _methodName, "Unknown Exception: ", ex);
			   }
		  }

		  TraderLogger.logMethodExit(_className, _methodName);
	 }

	 public boolean isResultsFound()
	 {
		  return resultsFound;
	 }

	 public boolean isSaleFavourited()
	 {
		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No results have been found.");
		  }

		  return saleFavouritedByUser;
	 }

}
